﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using KBS8.Shared;

namespace KBS8.Data.GdfDataFilter
{
    class PointFeatureReader: DatabaseReader
    {
        private string _query;

        public PointFeatureReader(SqlCeConnection connection) : base(connection)
        {
            
        }

        /// <summary>
        /// Reads the PointFeatures in the database
        /// </summary>
        /// <param name="o"></param>
        public override void Read(FilteredOutput o)
        {
            _query = @"SELECT * 
                     FROM PointFeature AS pf 
                     INNER JOIN NewNode AS nn ON pf.NodeId =  nn.NewNodeId
                     INNER JOIN Coordinate AS c ON nn.CoordinateSetId = c.CoordinatesId AND c.X BETWEEN " + o.Area.X + " AND " + (o.Area.X + o.Area.rec.Width).ToString() + @"
                     AND c.Y BETWEEN " + o.Area.Y + " AND " + (o.Area.Y + o.Area.rec.Height).ToString();

            

            Connection.Open();
            SqlCeCommand cmd = new SqlCeCommand(_query, Connection);
            SqlCeDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                PointFeature pf = new PointFeature();
                pf.FeatureId = Convert.ToInt32(reader["FeatureDefenitionId"]);
                pf.NodeId = Convert.ToInt32(reader["NodeId"]);
                pf.PointId = Convert.ToInt32(reader["PointFeatureId"]);
                pf.X = Convert.ToInt32(reader["X"]);
                pf.Y = Convert.ToInt32(reader["Y"]);
                o.PointFeatures.Add(pf);
            }
        }
    }
}
